/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package work.konbai.basic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import work.konbai.basic.entity.Course;
import work.konbai.basic.service.ICourseService;
import work.konbai.basic.service.ICourseStudentService;
import work.konbai.basic.vo.CourseStudentVO;
import work.konbai.basic.vo.CourseVO;
import work.konbai.basic.vo.MemberVO;
import work.konbai.basic.wrapper.CourseWrapper;

import javax.validation.Valid;

/**
 * 课程信息表 控制器
 *
 * @author KonBAI
 * @since 2021-01-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/course")
@Api(value = "课程信息表", tags = "课程信息表接口")
public class CourseController extends BladeController {

	private final ICourseService courseService;

	private final ICourseStudentService courseStudentService;

	/**
	* 详情
	*/
	@GetMapping("/detail")
    @ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入course")
	public R<Course> detail(Course course) {
		return R.data(courseService.getOne(Condition.getQueryWrapper(course)));
	}

	/**
	* 分页 课程信息表
	*/
	@GetMapping("/list")
    @ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入course")
	public R<IPage<Course>> list(Course course, Query query) {
		return R.data(courseService.page(Condition.getPage(query), Condition.getQueryWrapper(course)));
	}

	/**
	* 新增 课程信息表
	*/
	@PostMapping("/save")
    @ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入course")
	public R save(@Valid @RequestBody Course course) {
		return R.status(courseService.save(course));
	}

	/**
	* 修改 课程信息表
	*/
	@PostMapping("/update")
    @ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入course")
	public R update(@Valid @RequestBody Course course) {
		return R.status(courseService.updateById(course));
	}

	/**
	* 新增或修改 课程信息表
	*/
	@PostMapping("/submit")
    @ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入course")
	public R submit(@Valid @RequestBody Course course) {
		return R.status(courseService.saveOrUpdate(course));
	}


	/**
	* 删除 课程信息表
	*/
	@PostMapping("/remove")
    @ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(courseService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 分页 查询学生已拥有课程信息表
	 */
	@GetMapping("/own-list")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "分页", notes = "传入studentId")
	public R<IPage<CourseStudentVO>> ownList(@ApiParam(value = "学生ID", required = true) @RequestParam Long studentId, Query query) {
		return R.data(courseStudentService.ownPage(Condition.getPage(query), studentId));
	}

	/**
	 * 分页 查询可选课程信息表
	 */
	@GetMapping("/optional-list")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "分页", notes = "传入studentId")
	public R<IPage<CourseVO>> optionalList(@ApiParam(value = "学生ID", required = true) @RequestParam Long studentId, Query query) {
		IPage<Course> pages = courseStudentService.optionalPage(Condition.getPage(query), studentId);
		return R.data(CourseWrapper.build().pageVO(pages));
	}

	/**
	 * 分页 课程成员信息表
	 */
	@SneakyThrows
	@GetMapping("/member-list")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "分页", notes = "传入courseId")
	public R<IPage<MemberVO>> memberList(@ApiParam(value = "课程ID", required = true) @RequestParam Long courseId, Query query) {
		return R.data(courseStudentService.memberPage(Condition.getPage(query), courseId));
	}

	/**
	 * 分页 查询课堂信息
	 */
	@GetMapping("/classroom-list")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "分页", notes = "传入query")
	public R<IPage<CourseVO>> classroomList(Query query) {
		IPage<Course> pages = courseService.page(Condition.getPage(query),
			Wrappers.<Course>lambdaQuery().eq(Course::getTeacherId, SecureUtil.getUserId()));
		return R.data(CourseWrapper.build().pageVO(pages));
	}
}
